var paginationTotal = 0;//记录总数
$(function(){
	initData(0); //初始化分页数据
	getOperation(1);	//初始状态为草稿的操作
    $("#ProductDiv").dialog({
		autoOpen:false,
		width:600,
		height:400,
		modal:true,
		show:"slide",
        zIndex:100
	});
	
	$(".editIcon").live("click",function(){
		window.open("/merchant/orders/edit/orderId/"+$(this).attr("orderid"),"_blank");
	});
	$(".viewIcon").live("click",function(){
		window.open("/merchant/orders/view/orderId/"+$(this).attr("orderid"),"_blank");
	});
	//回车处理
	$("#orders_code_sch").add("#ReferenceNo").add("#createTime").keyup(function(e){
		var key = e.which;
		if(key==13){
			search();
		}
	});
	
});

function loadData(page,pageSize){
    var orders_code_sch = $('[name=orders_code_sch]').val();
    var ReferenceNo = $('[name=ReferenceNo]').val();	//对应 客户参考号customer_order_code
    var orders_status = $('[name=status]').val();
    var underreview_status = $('[name=underreview_status]').val();
    var createTime = $('[name=createTime]').val();
    var shipping_method = $('[name=shipping_method]').val();
	$.ajax({
		type: "POST",
		async: false,
		dataType: "json",
		url: "/merchant/orders/index",
		data:{
			'page':page,
			'pageSize':pageSize,
            'orders_code_sch':orders_code_sch,
            'ReferenceNo':ReferenceNo,
            'orders_status':orders_status,
            'underreview_status':underreview_status,
            'shipping_method':shipping_method,
            'createDate':createTime
		},
		success: function(json) {
			//不同状态不同的列表头
			var headerHtml = '';			
			if(orders_status == 0){
				headerHtml = $("#order-header-content-0").html();
			}
			if(orders_status == 1 || orders_status == 2){
                headerHtml = $("#order-header-content-1").html();
			}
			if(orders_status == 3){
                headerHtml = $("#order-header-content-3").html();
			}
			if(orders_status == 4){
                headerHtml = $("#order-header-content-4").html();
			}
			if(orders_status == 5){
                headerHtml = $("#order-header-content-5").html();
			}
			if(orders_status == 6){
                headerHtml = $("#order-header-content-6").html();
			}

			$(".row-header").html(headerHtml);
			var html = "";
			if(json.ask != 1) {
				html = '<div class="line1">&nbsp;No Result</div>';
			}else {
                paginationTotal = json.total;
                $.each(json.result,function(key,val){
					html += '<div class="row height25">';
					
					if(orders_status == 0){
						html += '<div  class="cell" style="width:50px;">';
						html += "<input type='checkbox' value='" + val.orders_code + "' class='checked' name='orderids' />&nbsp;</div>";
						html += '<div  class="cell orderCode" style="width:150px;" title="'+val.orders_code+'" orderId="'+ val.order_id + '">'+val.orders_code+'</div>';
						html += '<div  class="cell " style="width:150px;" title="'+val.customer_order_code+'">'+val.customer_order_code+'</div>';
						html += '<div  class="cell " style="width:150px;" title="'+val.shipping_method+'">'+val.shipping_method+'</div>';
						html += '<div  class="cell " style="width:150px;" title="'+val.consignee_country+'">'+val.consignee_country+'</div>';
						html += '<div  class="cell " style="width:150px;" title="'+val.add_time+'">'+val.add_time+'</div>';
						html += '<div class="cell no-background" style="width:100px; padding-top:4px;">' +
								'<img src="/images/icon_edit.gif" title="edit" class="editIcon" orderid="'+val.order_id+'"/>' +
								'<img src="/images/icon_view.gif" title="view" class="viewIcon" orderid="'+val.order_id+'"/>' +
										'</div>';
						html += '</div>';
					}
					if(orders_status == 1){
						html += '<div  class="cell" style="width:50px;">';
						html += "<input type='checkbox' value='" + val.orders_code + "' class='checked' name='orderids' />&nbsp;</div>";
						html += '<div  class="cell orderCode" style="width:150px;" title="'+val.orders_code+'" orderId="'+ val.order_id + '">'+val.orders_code+'</div>';
						html += '<div  class="cell " style="width:150px;" title="'+val.customer_order_code+'">'+val.customer_order_code+'</div>';
						html += '<div  class="cell " style="width:100px;" title="'+val.shipping_method+'">'+val.shipping_method+'</div>';
						html += '<div  class="cell " style="width:150px;" title="'+val.consignee_country+'">'+val.consignee_country+'</div>';
						html += '<div  class="cell " style="width:100px;" title="'+val.shipping_fee_estimate+'">'+val.shipping_fee_estimate+'</div>';
						html += '<div  class="cell " style="width:150px" title="'+val.add_time+'">'+val.add_time+'</div>';
						html += '<div class="cell no-background" style="width:50px;; padding-top:4px;">' +
								'<img src="/images/icon_edit.gif" title="edit" class="editIcon" orderid="'+val.order_id+'"/>' +
								'<img src="/images/icon_view.gif" title="view" class="viewIcon" orderid="'+val.order_id+'"/>' +
										'</div>';
						html += '</div>';
					}
					if(orders_status == 2){
						html += '<div  class="cell" style="width:50px;">';
						html += "<input type='checkbox' value='" + val.orders_code + "' class='checked' name='orderids' />&nbsp;</div>";
						html += '<div  class="cell orderCode" style="width:150px;" title="'+val.orders_code+'" orderId="'+ val.order_id + '">'+val.orders_code+'</div>';
						html += '<div  class="cell " style="width:150px;" title="'+val.customer_order_code+'">'+val.customer_order_code+'</div>';
						html += '<div  class="cell " style="width:100px;" title="'+val.shipping_method+'">'+val.shipping_method+'</div>';
						html += '<div  class="cell " style="width:150px;" title="'+val.consignee_country+'">'+val.consignee_country+'</div>';
						html += '<div  class="cell " style="width:100px;" title="'+val.shipping_fee_estimate+'">'+val.shipping_fee_estimate+'</div>';
						html += '<div  class="cell " style="width:150px;" title="'+val.add_time+'">'+val.add_time+'</div>';
						html += '<div class="cell no-background" style="width:50px; padding-top:4px;">' +
								'<img src="/images/icon_edit.gif" title="edit" class="editIcon" orderid="'+val.order_id+'"/>' +
								'<img src="/images/icon_view.gif" title="view" class="viewIcon" orderid="'+val.order_id+'"/>' +
										'</div>';
						html += '</div>';
					}
					if(orders_status == 3){	
								html += '<div  class="cell" style="width:50px;">';
								html += "<input type='checkbox' value='" + val.orders_code + "' class='checked' name='orderids' />&nbsp;</div>";
								html += '<div  class="cell orderCode" style="width:150px;" title="'+val.orders_code+'" orderId="'+ val.order_id + '">'+val.orders_code+'</div>';
								html += '<div  class="cell " style="width:150px;" title="'+val.customer_order_code+'">'+val.customer_order_code+'</div>';
								html += '<div  class="cell " style="width:150px;" title="'+val.shipping_method+'">'+val.shipping_method+'</div>';
								html += '<div  class="cell " style="width:150px;" title="'+val.consignee_country+'">'+val.consignee_country+'</div>';
								html += '<div  class="cell " style="width:150px;" title="'+val.shipping_fee_estimate+'">'+val.shipping_fee_estimate+'</div>';
								
								html += '<div class="cell no-background" style="width:100px;; padding-top:4px;">' +
										'<img src="/images/icon_view.gif" class="viewIcon" orderid="'+val.order_id+'"/>' ;
								if(val.process_status==1){
									html +='<img src="/images/cancel.png" onclick="orderStop(this);" class="orderBack" orders_code="'+val.orders_code+'"/>' ;
								}else if(val.process_status==2 || val.process_status==3 || val.process_status==4 ){
									html +='<img src="/images/cancel.png" onclick="orderStop(this);" class="orderBack" orders_code="'+val.orders_code+'"/>' ;
								}

								html +='</div>';
								html += '</div>';
									
						
					}
					if(orders_status == 4){
						html += '<div  class="cell" style="width:50px;">';
						html += "<input type='checkbox' value='" + val.orders_code + "' class='checked' name='orderids' />&nbsp;</div>";
						html += '<div  class="cell orderCode" style="width:150px;" title="'+val.orders_code+'" orderId="'+ val.order_id + '">'+val.orders_code+'</div>';
						html += '<div  class="cell " style="width:150px;" title="'+val.customer_order_code+'">'+val.customer_order_code+'</div>';
						html += '<div  class="cell " style="width:100px;" title="'+val.shipping_method+'">'+val.shipping_method+'</div>';
						html += '<div  class="cell " style="width:150px;" title="'+val.add_time+'">'+val.add_time+'</div>';
						html += '<div  class="cell " style="width:150px" title="'+val.last_modified_time+'">'+val.last_modified_time+'</div>';
						var underreview = val.underreview_status==1?"Out of stock":(val.underreview_status==2?"not sufficient funds":"--");
						html += '<div  class="cell " style="width:100px;" title="">'+underreview+'</div>';
						html += '<div class="cell no-background" style="width:50px; padding-top:4px;">' +
								'<img src="/images/icon_edit.gif" class="editIcon" orderid="'+val.order_id+'"/>' +
								'<img src="/images/icon_view.gif" class="viewIcon" orderid="'+val.order_id+'"/>' +
										'</div>';
						html += '</div>';
					}
                   var tracking_number='';
                    if(val.tracking_number && val.tracking_number!=undefined ){
                        tracking_number=val.tracking_number;
                    }
					if(orders_status == 5){
						html += '<div  class="cell orderCode" style="width:150px;" title="'+val.orders_code+'" orderId="'+ val.order_id + '">'+val.orders_code+'</div>';
						html += '<div  class="cell " style="width:150px;" title="'+val.customer_order_code+'">'+val.customer_order_code+'</div>';
						html += '<div  class="cell " style="width:150px;" title="'+val.shipping_method+'">'+val.shipping_method+'</div>';
						html += '<div  class="cell " style="width:100px;" title="'+val.consignee_country+'">'+val.consignee_country+'</div>';
						html += '<div  class="cell " style="width:150px;" title="'+tracking_number+'">'+tracking_number+'</div>';
						html += '<div  class="cell " style="width:150px;" title="'+val.add_time+'">'+val.add_time+'</div>';
                        html += '<div class="cell2 no-background" style="width:50px; padding-top:4px;">' +
                        								'<img src="/images/icon_view.gif" class="viewIcon" orderid="'+val.order_id+'"/>' +
                        								'</div>';
						html += '</div>';
					}
					if(orders_status == 6){
						html += '<div  class="cell orderCode" style="width:150px;" title="'+val.orders_code+'" orderId="'+ val.order_id + '">'+val.orders_code+'</div>';
						html += '<div  class="cell " style="width:150px;" title="'+val.customer_order_code+'">'+val.customer_order_code+'</div>';
						html += '<div  class="cell " style="width:100px;" title="'+val.shipping_method+'">'+val.shipping_method+'</div>';
                        html += '<div  class="cell " style="width:150px;" title="'+tracking_number+'">'+tracking_number+'</div>';
						html += '<div  class="cell " style="width:150px;" title="'+val.ship_date+'">'+val.ship_date+'</div>';
						html += '<div  class="cell2 " style="width:150px;" title="'+val.add_time+'">'+val.add_time+'</div>';
                        html += '<div class="cell2 no-background" style="width:50px; padding-top:4px;">' +
                        								'<img src="/images/icon_view.gif" class="viewIcon" orderid="'+val.order_id+'"/>' +
                        								'</div>';
						html += '</div>';
					}
                	html += '<div style="width:100%;border-bottom:1px solid #cccccc;display:none;font-style:italic;background-color:#CAD9EA;" id="detailInfo'+val.order_id+'" class="detailInfo">';
					html += '<div class="row height25 bold"><div class="cell" style="width:16%">SKUName</div><div class=" cell" style="width:15%">SKU</div><div class="cell" style="width:25%">Category</div><div class="cell" style="width:20%">Weight(Kg)</div><div class="cell" style="width:15%">Quantity</div></div>';
					$.each(val.products,function(pK,pV){
						html += '<div class="row height25"><div class="cell" style="width:16%"><a href="/merchant/product/view-product-info/productId/'+pV.product_id+'/edit/false" target="_blank">'+pV.product_title+'</a></div><div class=" cell" style="width:15%">'+pV.product_sku+'</div><div class="cell" style="width:25%">'+pV.category.poc_value+'</div><div class="cell" style="width:20%">'+pV.product_weight+'</div><div class="cell" style="width:15%">'+pV.quantity+'</div></div>';
					});
					html += '</div>';
                });
			}
			$("#order-list").html(html);	
			$(".orderCode").click(function(){
				$("#detailInfo"+$(this).attr("orderid")).toggle();
			});
			$("#status0").html(json.statusTotal.deleteTotal);
			$("#status1").html(json.statusTotal.draftTotal);
			$("#status2").html(json.statusTotal.confirmTotal);
			$("#status3").html(json.statusTotal.insufficientTotal);
			$("#status4").html(json.statusTotal.proccesingTotal);
			$("#status5").html(json.statusTotal.shippedTotal);
			$("#status6").html(json.statusTotal.deliveredTotal);
		}
	});
	
}

function search() {
   initData(0);
}



function del(sp_id,name){
    $('#confrimDiv').dialog("close");
    $.ajax({
        type: "POST",
        async: false,
        dataType: "json",
        url: "/shipping/ship-price/delete", //请求的处理url
        data: {'sp_id':sp_id},
        success: function(json){
            var html ="";
            if(json.ask){
                var html ='<p class="alertMsg"><img src="/images/yes.gif">'+json.list.message+'</p>';
                $('#message').html(html);
            }else{
                var error= json.list.error;
                $.each(error,function(key,item){
                    html += '<p class="alertMsg"><img src="/images/no.gif">'+item+'</p>';
                });
                $('#message').html(html);
            }
            $('.messageDiv').dialog("open");
            var curPage= $(".pagination span> .current").html();
            initData(curPage);
        }
    });
}

function back(sp_id,name){
    $('#confrimDiv').dialog("close");
    $.ajax({
        type: "POST",
        async: false,
        dataType: "json",
        url: "/shipping/ship-price/delete", //请求的处理url
        data: {'sp_id':sp_id},
        success: function(json){
            var html ="";
            if(json.ask){
                var html ='<p class="alertMsg"><img src="/images/yes.gif">'+json.list.message+'</p>';
                $('#message').html(html);
            }else{
                var error= json.list.error;
                $.each(error,function(key,item){
                    html += '<p class="alertMsg"><img src="/images/no.gif">'+item+'</p>';
                });
                $('#message').html(html);
            }
            $('.messageDiv').dialog("open");
            var curPage= $(".pagination span> .current").html();
            initData(curPage);
        }
    });
}
